<style>
	@import url("../../common/css/order.css");
</style>
<template>
	<view>
		<commonHeader :titleName="titleName"></commonHeader>
		<view class="content_main_nav">
			
			<view class="address_info" v-for="(item, index) in shops">
			
			
				<view class=""  :id="item.shopId"  @click="clickCard">
					<uni-card  @click="clickCard" :id="1234"
					    :title="item.shopName"
					    mode="style"
					    :is-shadow="true"
					    :thumbnail="item.shopPic"
					    extra="点击选择此自提点 >>"
					    
					>
					       <view class="" @click="phone" :id="item.phone" style="text-decoration: underline;">
					       电话： {{$praseStrEmpty(item.phone)}}
					       </view>
						   <view class="">
						   	地址： {{$praseStrEmpty(item.address)}}
						   </view>
						   <view class="">
						   	距离：{{$praseStrEmpty(item.distance)}} 公里
						   </view>
					</uni-card>
					
				</view>
			</view>
	
		
		
		</view>
		
			
		
	</view>	
</template>

<script>
	import commonHeader from '@/components/common-header/common_header_title.vue';
	import timePicker from '@/components/wing-time-selector/wing-time-selector.vue';
	export default {
		components:{
			commonHeader,timePicker
		},
		data() {
			return {
				shops:'',//带距离的店铺信息
				shop:{},//最近的店
				
				hidden:true,
				shop_id:'',
				titleName:'自提点列表',
				leaveText:'',
				orderGoodsList:[],
				showGoodsStr:'',
				shopStr:'',
				showGoodsArr:[],
				orderInfo:{},
				chooseDateIndex:1,
				dateList:[],
				selectDateStr:'',
				selectDate:0,
				pDay:0,
				pHour:0,
				pMinute:0,
				shippingInfo:{},//配送信息
				totalMoney:0,
				goodsMoney:0,
				paotui_price:0,
				userVipInfo:{}
			}
		},
		onLoad(e) {
			let that = this
			that.event = that.getOpenerEventChannel()
		
			
			
			
			//请求代收点 获取缓存里的shops数据
			uni.getStorage({
			    key: 'shopss',
			    success: function (res) {
			        console.log(res.data)
					that.shops = res.data
				for (var i=0;i<that.shops.length;i++)
			{ 
			  if(that.shops[i].distance == 0){
				   that.shop.address=that.shops[i].address
				   that.shop.distance=that.shops[i].distance
				  that.shop.phone=that.shops[i].phone				 
				  that.shop.shopName= that.shops[i].shopName
				  that.shop.shopPic=that.shops[i].shopPic
				  
			  }
			}	
					
					
			    }
			});
			
			// let postData = {agentStatus:2,cscId:11}
			// 	that.$api.daishoudian.daishoudianList(postData)
			// 	.then(res => {
			// 		if(res.code == 200){
			// 			// this.scsList = res.data
			// 			console.log(res.data)
			// 		}else{
			// 			that.$commonJs.showToast.showNoneToast(res.err)
			// 		}
			// 	})
			// 	.catch(err => {
			// 		that.$commonJs.showToast.showNoneToast(err)
			// 	})
			console.log(e)
			
			
			
			
			let carItemList = JSON.parse(e.cartItemList)
			that.goodsMoney = e.money
			that.totalMoney = e.money
			console.log(carItemList)
			//店铺id
			console.log(carItemList[0].shopId)
			that.shop_id = carItemList[0].shopId
			if(carItemList[0].shopId ==  77){
				that.hidden = false
			}
			let store_id = -1
			carItemList.map((item) => {
				let jsonCartGoods  = {id:item.id,count:item.num}
				that.orderGoodsList.push(jsonCartGoods)
				if(that.showGoodsArr[item.shopId]){
					let arr = {shopName:item.shopName,shopGoodsList:item}
					that.showGoodsArr[item.shopId].push(arr)
				}else{
					that.showGoodsArr[item.shopId] = [{shopName:item.shopName,shopGoodsList:item}]
				}
			})
			if(that.showGoodsArr.length > 0){
				that.showGoodsArr.map((item) => {
					let shopName = item[0].shopName
					that.shopStr += shopName + ","
					that.showGoodsStr += shopName + ":"
					item.map((items) => {
						that.showGoodsStr += items.shopGoodsList.goodsName + "*" + items.shopGoodsList.num + "份,"
					})
					that.showGoodsStr = that.showGoodsStr.substring(0,that.showGoodsStr.length - 1)
					that.showGoodsStr += "\r\n"
				})
			}
			that.shopStr = that.shopStr.substring(0,that.shopStr.length - 1)
			this.getDateInfo()
			this.getUserVipInfo()
		},
		onShow(){
			this.getDefaultAddress()
		},
		methods: {
			//拨打电话
			phone(e){
				console.log(e.currentTarget.id)
				uni.makePhoneCall({
				    phoneNumber: e.currentTarget.id //仅为示例
				})
			},
			
			//点击卡片
			clickCard(e){
				let that = this
				console.log(e.currentTarget.id)
				uni.navigateBack({
				    delta: 1,
					success:()=>{
						that.event.emit('data2',e.currentTarget.id)
					}
				});
				
			},
			getNDay(day) {
				var today = new Date();　　
				var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;　　
				today.setTime(targetday_milliseconds); //注意，这行是关键代码
				　　
				var tYear = today.getFullYear();　　
				var tMonth = today.getMonth();　　
				var tDate = today.getDate();　　
				tMonth = this.doHandleMonth(tMonth + 1);　　
				tDate = this.doHandleMonth(tDate);　　
				return tYear + "-" + tMonth + "-" + tDate;
			},
			getDateInfo(){
				let that = this
				this.$api.order.payOrderList()
				.then(res => {
					if(res.code == 200){
						that.dateList = res.data
						if(that.shop_id == 77){
							that.paotui_price = 0
						}else{
							that.paotui_price = res.data[0].amount
						}
						
						that.totalMoney 	=	(that.totalMoney * 100 + that.paotui_price * 100)/100
						console.log(that.totalMoney)
						that.dateList.push(yyps)
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			},
			getUserVipInfo(){
				let that = this
				this.$api.vip.getVipInfo()
				.then(res => {
					console.log(res)
					if(res.code == 200){
						console.log(res.data)
						that.userVipInfo = res.data
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast('会员卡信息有误，请联系工作人员')
				})
			},
			doHandleMonth(month) {　　
				var m = month;　　
				if (month.toString().length == 1) {　　
					m = "0" + month;　　
				}　　
				return m;
			},
			goToEditAddress(){
				this.$uniNavigateTo('../../../pages/address/address-list/address-list')
			},
			getDefaultAddress(){
				let that = this
				this.$api.address.userDefaultAddress()
				.then(res => {
					if(res.code == 200){
						let resData = res.data
						if(resData.length <= 0){
							//跳转地址
							this.$commonJs.waitFunction.waitNavigateTo('请先设置收货地址','../../../pages/address/address-list/address-list')
						}else{
							that.shippingInfo = resData[0]
						}
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			},
			chooseDate(e,index){
				let that = this
				that.chooseDateIndex = e.id
				if(that.userVipInfo.id == -1 || that.userVipInfo.usageTimes <= 0){
					that.paotui_price = e.amount
					that.totalMoney = that.goodsMoney * 1  + e.amount * 1
					that.totalMoney = this.$commonJs.numberFilter.twoNumherFilter(that.totalMoney)
				}
				
				if(that.chooseDateIndex != -1){
					that.selectDateStr = e.name
					that.selectDate = that.getNDay(0)
				}
			},
			btnConfirm(e) {
				console.log(e)
				let that = this
				that.selectDateStr = e.key
				that.selectDate = e.year_date
				if(e.n_year == '今天'){
					that.pDay = 0
				}else if(e.n_year == '明天'){
					that.pDay = 1
				}else if(e.n_year == '后天'){
					that.pDay = 2
				}
				that.pHour = e.hour
				that.pMinute = e.minute
			},
			payOrder(){
				if(this.chooseDateIndex == 0){
					this.$toast("请选择送达时间");
					return false
				}
				if(this.$store.state.txMap.scsId == -1){
					this.$toast("请选择社区再进行下单")
					return false
				}
				console.log(this.orderGoodsList)
				let postData = {}
				console.log(this.shop_id)
				console.log(this.chooseDateIndex)
				if(this.shop_id == 77){
					console.log("团购店订单")
					if(this.chooseDateIndex == -1){
						postData = {cscId:this.$store.state.txMap.scsId,type:2,distributionType:this.chooseDateIndex,customTimeJson:{day:this.pDay,minute:this.pMinute,hour:this.pHour},goodsIds:this.orderGoodsList,addressId:this.shippingInfo.id,orderRemark:this.leaveText}
					}else{
						postData = {cscId:this.$store.state.txMap.scsId,type:2,distributionType:this.chooseDateIndex,goodsIds:this.orderGoodsList,addressId:this.shippingInfo.id,orderRemark:this.leaveText}
					}
					if(this.chooseDateIndex == -1){
						postData = {cscId:this.$store.state.txMap.scsId,type:2,distributionType:this.chooseDateIndex,customTimeJson:{day:this.pDay,minute:this.pMinute,hour:this.pHour},goodsIds:this.orderGoodsList,addressId:this.shippingInfo.id,orderRemark:this.leaveText}
					}else{
						postData = {cscId:this.$store.state.txMap.scsId,type:2,distributionType:this.chooseDateIndex,goodsIds:this.orderGoodsList,addressId:this.shippingInfo.id,orderRemark:this.leaveText}
					}
				}else{
					console.log("普通订单")
					if(this.chooseDateIndex == -1){
						postData = {cscId:this.$store.state.txMap.scsId,type:0,distributionType:this.chooseDateIndex,customTimeJson:{day:this.pDay,minute:this.pMinute,hour:this.pHour},goodsIds:this.orderGoodsList,addressId:this.shippingInfo.id,orderRemark:this.leaveText}
					}else{
						postData = {cscId:this.$store.state.txMap.scsId,type:0,distributionType:this.chooseDateIndex,goodsIds:this.orderGoodsList,addressId:this.shippingInfo.id,orderRemark:this.leaveText}
					}
					if(this.chooseDateIndex == -1){
						postData = {cscId:this.$store.state.txMap.scsId,type:0,distributionType:this.chooseDateIndex,customTimeJson:{day:this.pDay,minute:this.pMinute,hour:this.pHour},goodsIds:this.orderGoodsList,addressId:this.shippingInfo.id,orderRemark:this.leaveText}
					}else{
						postData = {cscId:this.$store.state.txMap.scsId,type:0,distributionType:this.chooseDateIndex,goodsIds:this.orderGoodsList,addressId:this.shippingInfo.id,orderRemark:this.leaveText}
					}
				}
				// if(this.chooseDateIndex == -1){
				// 	postData = {cscId:this.$store.state.txMap.scsId,type:0,distributionType:this.chooseDateIndex,customTimeJson:{day:this.pDay,minute:this.pMinute,hour:this.pHour},goodsIds:this.orderGoodsList,addressId:this.shippingInfo.id,orderRemark:this.leaveText}
				// }else{
				// 	postData = {cscId:this.$store.state.txMap.scsId,type:0,distributionType:this.chooseDateIndex,goodsIds:this.orderGoodsList,addressId:this.shippingInfo.id,orderRemark:this.leaveText}
				// }
				console.log(this.userVipInfo.id)
				if(this.userVipInfo.id != -1 && this.userVipInfo.usageTimes > 0){
					this.$set(postData,'distfFeePaymentType',1)
					this.$set(postData,'userMembershipCardId',this.userVipInfo.id)
				}else{
					this.$set(postData,'distfFeePaymentType',0)
				}
				this.$api.order.payOrderAdd(postData)
				.then(res => {
					if(res.code == 200){
						//拉起支付
						if(res.data.isPay == '0'){
							this.$commonJs.waitFunction.waitRedirectTo(res.msg,'/pages/order/index')
						}else{
							this.$payCommon.xcxPay(res.data)
						}
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			}
		}
	}
</script>
